﻿using System;
using Liber;
using Liber.SOA;
using Liber.ORM;
using System.Collections.Generic;
using Liber.BizTools;

namespace SoBusiness.App
{
    public class MineApp:SOAUnit
    {
        public MineApp(SOAHost _host):base(_host)
        {
        }

        [API("GetUserInfo")]
        FnResult<DyObject> GetUserInfo(SOARequestContext _rq)
        {
            long acntId = _rq.RequestContext.GetOrDefault<long>("acntid", -1);
            if (acntId == -1)
            {
                return FnResult<DyObject>.Failure(Name, "RequiredArgs").SetError("acntid", "empty");
            }
            var uinfo = new DyObject();

            var acnt = LiberBizHelper.AcntSVC.GetAccountInfo(acntId,true);
            if (acnt.IsPositive)
            {
                uinfo = acnt.Data;
            }
            else
            {
                return acnt;
            }

            var orgs = LiberBizHelper.OrgSVC.GetAcntBelongOrgs(acntId);
            uinfo.Set("Orgs", orgs);

            var authOrgs = LiberBizHelper.OrgSVC.GetAcntOwnOrgs(acntId);
            uinfo.Set("AuthedOrgs", authOrgs);

            var pos = LiberBizHelper.OrgSVC.GetAcntPositions(acntId);
            uinfo.Set("Positions", pos);

            var auth = LiberBizHelper.AuthoritySVC.GetAcntAuthority(acntId);
            uinfo.Set("Authority", auth);

            return FnResult<DyObject>.Sucess(uinfo);
        }

        [API("ApplyAcntCertify")]
        FnResult<DyObject> ApplyAcntCertify(SOARequestContext rq)
        {
            long uid = rq.RequestContext.GetOrDefault<long>("uid", -1);
            string name = rq.RequestContext.GetOrDefault<string>("name", "");
            string phone = rq.RequestContext.GetOrDefault<string>("phone", "");
            string sid = rq.RequestContext.GetOrDefault<string>("socialid", "");
            string mail = rq.RequestContext.GetOrDefault<string>("mail", "");
            string idcard = rq.RequestContext.GetOrDefault<string>("idcardface", "");

            var nac = LiberBizHelper.AcntSVC.CreatePeople(uid, name,sid, phone, mail, idcard);
            return nac;
        }

        [API("UpdateSetting")]
        FnResult<DyObject> UpdateSetting(SOARequestContext rq)
        {
            long uid = rq.RequestContext.GetOrDefault<long>("uid", -1);
            string ns = rq.RequestContext.GetOrDefault<string>("setting", "");
            LiberBizHelper.AcntSVC.UpdateSetting(uid, ns);
            return FnResult<DyObject>.Sucess(null);
        }

        [API("ChangePwd")]
        FnResult<DyObject> ChangePwd(SOARequestContext rq)
        {
            long uid = rq.RequestContext.GetOrDefault<long>("uid", -1);
            string opwd = rq.RequestContext.GetOrDefault<string>("oldpwd", "");
            string npwd = rq.RequestContext.GetOrDefault<string>("newpwd", "");
            return LiberBizHelper.AcntSVC.ResetPwd(uid, opwd, npwd);
        }

        [API("UpdateAcnt")]
        FnResult<DyObject> UpdateAcnt(SOARequestContext rq)
        {
            long uid = rq.RequestContext.GetOrDefault<long>("uid", -1);
            string phone = rq.RequestContext.GetOrDefault<string>("newphone", "");
            string hi = rq.RequestContext.GetOrDefault<string>("headimg", "");

            return LiberBizHelper.AcntSVC.ResetAcnt(uid, phone,hi);
        }

        [API("GetCertification")]
        FnResult<DyObject> GetCertification(SOARequestContext rq)
        {
            long uid = rq.RequestContext.GetOrDefault<long>("uid", -1);
            var p= LiberBizHelper.AcntSVC.GetPeople(uid);
            return p == null ? FnResult<DyObject>.Failure(Name, "NoCertification") : FnResult<DyObject>.Sucess(p);
        }

        [API("BindOpenUser")]
        FnResult<DyObject> BindOpenUser(SOARequestContext rq)
        {
            long uid = rq.RequestContext.GetOrDefault<long>("uid", -1);
            string opensrc = rq.RequestContext.GetOrDefault<string>("opensrc", "");
            string sex = rq.RequestContext.GetOrDefault<string>("opensex","Female");
            string name = rq.RequestContext.GetOrDefault<string>("openname", "");
            string headimg = rq.RequestContext.GetOrDefault<string>("openheadimg", "");
            string id = rq.RequestContext.GetOrDefault<string>("openid", "");

            return LiberBizHelper.AcntSVC.BindOpenUser(uid, opensrc, id, headimg, name);

        }

        /// <summary>
        /// 提供给第三方检查是否有个人未完成的待办工作
        /// </summary>
        /// <param name="rq"></param>
        /// <returns></returns>
        [API("OpenCountMyTODO")]
        FnResult<DyObject> OpenCountTodo(SOARequestContext rq)
        {
            string src = rq.RequestContext.GetOrDefault<string>("opensrc", "");
            string usr = rq.RequestContext.GetOrDefault<string>("openuser", "");
            var acnt = LiberBizHelper.AcntSVC.GetAcntInfoByOpenUser(src, usr);
            if (!acnt.IsPositive)
            {
                return acnt;
            }
            else
            {
                long uid = (long)acnt.Data["id"];
                List<DyObject> todos = LiberBizHelper.TODOSvc.CountUserTodo(uid,BizObjLifeStatus.Active);
                return FnResult<DyObject>.Sucess(DyObject.FromObjList("mytodo", todos));
            }
        }
    }

}
